package com.bang.study.util;

import java.util.Random;

/**
 * @Auther: Bang
 * @Date: 2019/9/21 0021 10:01
 * @Description: 数组工具类
 */
public class ArrayUtil {


    private static Random random = new Random();

    /**
     * 生成有n个元素的随机数组,每个元素的随机范围为[rangeL, rangeR]
     *
     * @param n
     * @param rangeL
     * @param rangeR
     * @return
     */
    public static Integer[] generateRandomArray(int n, int rangeL, int rangeR) {
        assert (rangeL <= rangeR);
        Integer[] arr = new Integer[n];
        for (int i = 0; i < n; i++)
            arr[i] = random.nextInt(rangeR) % (rangeR - rangeL + 1) + rangeL;

        return arr;
    }



    /**
     * 打印数组
     * @param arr
     * @param n
     */
    public static void printArray(Integer[] arr,int n) {
        StringBuilder sb = new StringBuilder("[ ");
        for (int i = 0; i < n; i++) {
            Integer t = arr[i];
            sb.append(t).append(" ");
        }
        sb.append("]");
        System.out.println(sb.toString());

    }

    public static void main(String[] args) {
        Integer[] ints = generateRandomArray(10, 0, 20);
       printArray(ints, ints.length);
    }
}
